Builder Pattern
   HOME

TheInfoList



OR:

The builder pattern is a
design pattern A design pattern is the re-usable form of a solution to a design problem. The idea was introduced by the architect Christopher Alexander and has been adapted for various other disciplines, particularly software engineering. The " Gang of Four" b ...
designed to provide a flexible solution to various object creation problems in
object-oriented programming Object-oriented programming (OOP) is a programming paradigm based on the concept of "objects", which can contain data and code. The data is in the form of fields (often known as attributes or ''properties''), and the code is in the form of pr ...
. The intent of the Builder design pattern is to separate the construction of a complex object from its representation. It is one of the Gang of Four design patterns.


Overview

The Builder design pattern is one of the ''
Design Patterns ''Design Patterns: Elements of Reusable Object-Oriented Software'' (1994) is a software engineering book describing software design patterns. The book was written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, with a foreword ...
'' that describe how to solve recurring design problems in object-oriented software. The Builder design pattern solves problems like: * How can a class (the same construction process) create different representations of a complex object? * How can a class that includes creating a complex object be simplified? Creating and assembling the parts of a complex object directly within a class is inflexible. It commits the class to creating a particular representation of the complex object and makes it impossible to change the representation later independently from (without having to change) the class. The Builder design pattern describes how to solve such problems: * Encapsulate creating and assembling the parts of a complex object in a separate Builder object. * A class delegates object creation to a Builder object instead of creating the objects directly. A class (the same construction process) can delegate to different Builder objects to create different representations of a complex object.


Definition

The intent of the Builder design pattern is to separate the construction of a complex object from its representation. By doing so, the same construction process can create different representations.


Advantages

Advantages of the Builder pattern include: * Allows you to vary a product's internal representation. * Encapsulates code for construction and representation. * Provides control over steps of construction process.


Disadvantages

Disadvantages of the Builder pattern include: * A distinct ConcreteBuilder must be created for each type of product. * Builder classes must be mutable. * May hamper/complicate dependency injection.


Structure


UML class and sequence diagram

In the above
UML The Unified Modeling Language (UML) is a general-purpose, developmental modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a system. The creation of UML was originally m ...
class diagram In software engineering, a class diagram in the Unified Modeling Language (UML) is a type of static structure diagram that describes the structure of a system by showing the system's classes, their attributes, operations (or methods), and the rela ...
, the Director class doesn't create and assemble the ProductA1 and ProductB1 objects directly. Instead, the Director refers to the Builder interface for building (creating and assembling) the parts of a complex object, which makes the Director independent of which concrete classes are instantiated (which representation is created). The Builder1 class implements the Builder interface by creating and assembling the ProductA1 and ProductB1 objects.
The
UML The Unified Modeling Language (UML) is a general-purpose, developmental modeling language in the field of software engineering that is intended to provide a standard way to visualize the design of a system. The creation of UML was originally m ...
sequence diagram A sequence diagram or system sequence diagram (SSD) shows process interactions arranged in time sequence in the field of software engineering. It depicts the processes involved and the sequence of messages exchanged between the processes needed ...
shows the run-time interactions: The Director object calls buildPartA() on the Builder1 object, which creates and assembles the ProductA1 object. Thereafter, the Director calls buildPartB() on Builder1, which creates and assembles the ProductB1 object.


Class diagram

;Builder :Abstract interface for creating objects (product). ;ConcreteBuilder :Provides implementation for Builder. It is an object able to construct other objects. Constructs and assembles parts to build the objects.


Examples

A C# example: /// /// Represents a product created by the builder /// public class Bicycle /// /// The builder abstraction /// public interface IBicycleBuilder /// /// Concrete builder implementation /// public class GTBuilder : IBicycleBuilder /// /// The director /// public class MountainBikeBuildDirector public class Client The Director assembles a bicycle instance in the example above, delegating the construction to a separate builder object that has been given to the Director by the Client.


See also

*
Currying In mathematics and computer science, currying is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. For example, currying a function f that ...
* FluentQueryBuilder


References


External links

{{Design Patterns Patterns Software design patterns Articles with example Java code